package com.splic.web.controller.product;

import java.util.List;
import com.splic.common.utils.StringUtils;
import com.splic.product.domain.*;
import com.splic.product.dto.OrderDto;
import com.splic.product.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.splic.common.annotation.Log;
import com.splic.common.enums.BusinessType;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.AjaxResult;
import com.splic.common.utils.poi.ExcelUtil;
import com.splic.common.core.page.TableDataInfo;

/**
 * 订单Controller
 * 
 * @author singe
 * @date 2022-01-04
 */
@Controller
@RequestMapping("/product/order")
public class OrderController extends BaseController
{
    private String prefix = "product/order";

    @Autowired
    private OrderService orderServiceImpl;
    @Autowired
    private GoodsService goodsServiceImpl;
    @Autowired
    private GoodsTypeService goodsTypeServiceImpl;
    @Autowired
    private ProductService productServiceImpl;
    @Autowired
    private ChannelService channelServiceImpl;
    @Autowired
    private DeviceStatisService deviceStatisServiceImpl;

    @RequiresPermissions("product:order:view")
    @GetMapping()
    public String order(ModelMap mmap)
    {
        List<Channel> channelList = channelServiceImpl.selectChannelList(null);
        mmap.put("channelList",channelList);
        List<Product> productList = productServiceImpl.selectProductList(null);
        mmap.put("productList",productList);
        List<String> phoneBrandList = deviceStatisServiceImpl.selectAddPhoneBrandList();
        mmap.put("phoneBrandList",phoneBrandList);
        List<String> phoneModelList = deviceStatisServiceImpl.selectAddPhoneModelList();
        mmap.put("phoneModelList",phoneModelList);
        return prefix + "/list";
    }

    /**
     * 根据选择应用查询服务
     */
    @GetMapping("/addChangeProduct")
    @ResponseBody
    public List<GoodsType> addChangeProduct(Long productId)
    {
        GoodsType goodsType = new GoodsType();
        goodsType.setProductId(productId);
        return goodsTypeServiceImpl.selectGoodsTypeList(goodsType);
    }
    /**
     * 根据选择应用查询服务套餐
     */
    @GetMapping("/addChangeGoodType")
    @ResponseBody
    public List<Goods> addChangeGoodType(Long goodTypeId)
    {
        GoodsQueryDto queryDto = new GoodsQueryDto();
        queryDto.setGoodsTypeId(goodTypeId);
        return goodsServiceImpl.selectGoodsList(queryDto);
    }

    /**
     * 查询订单列表
     */
    @RequiresPermissions("product:order:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderDto orderDto)
    {
        startPage();
        List<OrderDto> list = orderServiceImpl.selectPageList(orderDto);
        return getDataTable(list);
    }

    /**
     * 新增订单
     */
    @GetMapping(value = { "/form/{id}", "/form" })
    public String form(@PathVariable(value = "id", required = false) Long id, ModelMap mmap)
    {
        if (StringUtils.isNotNull(id)){
            mmap.put("order", orderServiceImpl.selectOrderById(id));
        }else{
            mmap.put("order", new Order());
        }
        return prefix + "/form";
    }


    /**
     * 新增保存订单
     */
    @RequiresPermissions("product:order:edit")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(Order order)
    {
        return toAjax(orderServiceImpl.saveOrUpdate(order));
    }

    /**
     * 删除
     */
    @RequiresPermissions("product:order:remove")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long id)
    {
        return toAjax(orderServiceImpl.deleteById(id));
    }


    /**
     * 导出订单列表
     */
    @RequiresPermissions("product:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderDto order)
    {
        List<Order> list = orderServiceImpl.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        return util.exportExcel(list, "订单数据");
    }

}
